/**
 * @author htqw
 * @param {import("mysql2").Connection} connection
 * @param {string} table
 * @param {Object.<string, string>} data
 */

export default async function insertTableData(connection, table, data) {
    return new Promise((resolve, reject) => {
        if (!data) return reject(new Error("No data to insert"))
        if (typeof data !== "object") return reject(new Error("Data must be an object"))
        const keys = Object.keys(data)
        const values = keys.map((key) => data[key])
        const query = `INSERT INTO ${table} (${keys.join(",")}) VALUES (${values.join(",")})`
        connection.query(query, (err, result) => {
            if (err) return reject(err)
            resolve(result)
        })
    })
}